﻿using System;
using System.Windows;
using System.Windows.Media.Animation;

namespace mediaPrime.Controls
{
    public class MPDialog : Window
    {
        private bool storyboardCompleted = false;
        private Nullable<bool> dialogResult;

        static MPDialog()
        {
            // Use another default style than the base class
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MPDialog), new FrameworkPropertyMetadata(typeof(MPDialog))
            );
        }

        public double ContentWidth
        {
            get { return (double)GetValue(ContentWidthProperty); }
            set { SetValue(ContentWidthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WindowWidth.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentWidthProperty =
            DependencyProperty.Register("ContentWidth", typeof(double), typeof(MPDialog), new PropertyMetadata(0.0));


        public double ContentHeight
        {
            get { return (double)GetValue(ContentHeightProperty); }
            set { SetValue(ContentHeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for WindowHeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentHeightProperty =
            DependencyProperty.Register("ContentHeight", typeof(double), typeof(MPDialog), new PropertyMetadata(0.0));




        public bool FadeBackground
        {
            get { return (bool)GetValue(FadeBackgroundProperty); }
            set { SetValue(FadeBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FadeBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FadeBackgroundProperty =
            DependencyProperty.Register("FadeBackground", typeof(bool), typeof(MPDialog), new PropertyMetadata(true));




        public MPDialog()
        {
            this.Initialized += MPDialog_Initialized;
            this.Loaded += MPDialog_Loaded;
            this.Closing += MPDialog_Closing;
        }

        private void MPDialog_Loaded(object sender, RoutedEventArgs e)
        {
            var storyboard = new Storyboard();

            var doubleAnimationFade = Template.Resources["doubleAnimationBackgroundFadeIn"] as DoubleAnimation;
            var doubleAnimationContent = Template.Resources["doubleAnimationContentFadeIn"] as DoubleAnimation;

            var doubleAnimationScaleX = Template.Resources["doubleAnimationContentScaleXIncrease"] as DoubleAnimation;
            var doubleAnimationScaleY = Template.Resources["doubleAnimationContentScaleYIncrease"] as DoubleAnimation;

            if (FadeBackground)
            {
                storyboard.Children.Add(doubleAnimationFade);
                storyboard.Children.Add(doubleAnimationContent);
            }

            storyboard.Children.Add(doubleAnimationScaleX);
            storyboard.Children.Add(doubleAnimationScaleY);

            storyboard.Begin(this, Template);
        }

        private void MPDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (!storyboardCompleted)
            {
                dialogResult = DialogResult;
                e.Cancel = true;

                var storyboard = new Storyboard();
                storyboard.Completed += storyboard_Completed;


                var doubleAnimationFade = Template.Resources["doubleAnimationBackgroundFadeOut"] as DoubleAnimation;
                var doubleAnimationScaleX = Template.Resources["doubleAnimationContentScaleXDecrease"] as DoubleAnimation;
                var doubleAnimationScaleY = Template.Resources["doubleAnimationContentScaleYDecrease"] as DoubleAnimation;
                var doubleAnimationContent = Template.Resources["doubleAnimationContentFadeOut"] as DoubleAnimation;

                if (FadeBackground)
                {
                    storyboard.Children.Add(doubleAnimationFade);
                    storyboard.Children.Add(doubleAnimationContent);
                }

                storyboard.Children.Add(doubleAnimationScaleX);
                storyboard.Children.Add(doubleAnimationScaleY);


                storyboard.Begin(this, Template);
            }

            DialogResult = dialogResult;
        }

        private void storyboard_Completed(object sender, EventArgs e)
        {
            storyboardCompleted = true;
            Close();
        }

        private void MPDialog_Initialized(object sender, EventArgs e)
        {
            SizeToContent = System.Windows.SizeToContent.Manual;

            ContentWidth = Width;
            ContentHeight = Height;

            Width = System.Windows.SystemParameters.PrimaryScreenWidth;
            Height = System.Windows.SystemParameters.PrimaryScreenHeight;

            Left = 0;
            Top = 0;
        }

        protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }

            base.OnPreviewKeyDown(e);
        }
    }
}
